<title>HAOI2015 树上操作</title><link href="http://hzoi.com/tomorrow.css" rel="stylesheet"><div class="ui existing segment"><pre><code><span class="pl-cp">#include</span> <span class="pl-cpf">&lt;bits/stdc++.h&gt;</span><span class="pl-cp"></span>
<span class="pl-k">using</span> <span class="pl-k">namespace</span> <span class="pl-n">std</span><span class="pl-p">;</span>
<span class="pl-k">const</span> <span class="pl-kt">int</span> <span class="pl-n">maxn</span> <span class="pl-o">=</span> <span class="pl-mi">500000</span> <span class="pl-o">+</span> <span class="pl-mi">10</span><span class="pl-p">;</span>
<span class="pl-kr">inline</span> <span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-nf">gi</span><span class="pl-p">()</span> <span class="pl-p">{</span>
    <span class="pl-kt">char</span> <span class="pl-n">ch</span> <span class="pl-o">=</span> <span class="pl-n">getchar</span><span class="pl-p">();</span>
    <span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">x</span> <span class="pl-o">=</span> <span class="pl-mi">0</span><span class="pl-p">,</span> <span class="pl-n">q</span> <span class="pl-o">=</span> <span class="pl-mi">0</span><span class="pl-p">;</span>
    <span class="pl-k">while</span> <span class="pl-p">(</span><span class="pl-n">ch</span> <span class="pl-o">&lt;</span> <span class="pl-sc">&#39;0&#39;</span> <span class="pl-o">||</span> <span class="pl-n">ch</span> <span class="pl-o">&gt;</span> <span class="pl-sc">&#39;9&#39;</span><span class="pl-p">)</span> <span class="pl-n">ch</span> <span class="pl-o">==</span> <span class="pl-sc">&#39;-&#39;</span> <span class="pl-o">?</span> <span class="pl-n">q</span> <span class="pl-o">=</span> <span class="pl-mi">1</span> <span class="pl-o">:</span> <span class="pl-mi">0</span><span class="pl-p">,</span> <span class="pl-n">ch</span> <span class="pl-o">=</span> <span class="pl-n">getchar</span><span class="pl-p">();</span>
    <span class="pl-k">while</span> <span class="pl-p">(</span><span class="pl-n">ch</span> <span class="pl-o">&gt;=</span> <span class="pl-sc">&#39;0&#39;</span> <span class="pl-o">&amp;&amp;</span> <span class="pl-n">ch</span> <span class="pl-o">&lt;=</span> <span class="pl-sc">&#39;9&#39;</span><span class="pl-p">)</span> <span class="pl-n">x</span> <span class="pl-o">=</span> <span class="pl-n">x</span> <span class="pl-o">*</span> <span class="pl-mi">10</span> <span class="pl-o">+</span> <span class="pl-n">ch</span> <span class="pl-o">-</span> <span class="pl-sc">&#39;0&#39;</span><span class="pl-p">,</span> <span class="pl-n">ch</span> <span class="pl-o">=</span> <span class="pl-n">getchar</span><span class="pl-p">();</span>
    <span class="pl-k">return</span> <span class="pl-n">q</span> <span class="pl-o">?</span> <span class="pl-p">(</span><span class="pl-o">-</span><span class="pl-n">x</span><span class="pl-p">)</span> <span class="pl-o">:</span> <span class="pl-n">x</span><span class="pl-p">;</span>
<span class="pl-p">}</span>
<span class="pl-cp">#define mid ((l + r) &gt;&gt; 1)</span>
<span class="pl-cp">#define lson rt &lt;&lt; 1, l, mid</span>
<span class="pl-cp">#define rson rt &lt;&lt; 1 | 1, mid + 1, r</span>
<span class="pl-cp">#define len (r - l + 1)</span>
<span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">n</span><span class="pl-p">,</span> <span class="pl-n">m</span><span class="pl-p">;</span>
<span class="pl-k">struct</span> <span class="pl-n">Edge</span> <span class="pl-p">{</span> <span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">next</span><span class="pl-p">,</span> <span class="pl-n">to</span><span class="pl-p">;</span> <span class="pl-p">}</span> <span class="pl-n">h</span><span class="pl-p">[</span><span class="pl-n">maxn</span> <span class="pl-o">&lt;&lt;</span> <span class="pl-mi">1</span><span class="pl-p">];</span>
<span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">head</span><span class="pl-p">[</span><span class="pl-n">maxn</span> <span class="pl-o">&lt;&lt;</span> <span class="pl-mi">1</span><span class="pl-p">];</span>
<span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">tot</span><span class="pl-p">,</span> <span class="pl-n">to</span><span class="pl-p">[</span><span class="pl-n">maxn</span><span class="pl-p">],</span> <span class="pl-n">w</span><span class="pl-p">[</span><span class="pl-n">maxn</span><span class="pl-p">],</span> <span class="pl-n">wt</span><span class="pl-p">[</span><span class="pl-n">maxn</span><span class="pl-p">];</span>
<span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">a</span><span class="pl-p">[</span><span class="pl-n">maxn</span> <span class="pl-o">&lt;&lt;</span> <span class="pl-mi">2</span><span class="pl-p">],</span> <span class="pl-n">laz</span><span class="pl-p">[</span><span class="pl-n">maxn</span> <span class="pl-o">&lt;&lt;</span> <span class="pl-mi">2</span><span class="pl-p">];</span>
<span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">son</span><span class="pl-p">[</span><span class="pl-n">maxn</span><span class="pl-p">],</span> <span class="pl-n">id</span><span class="pl-p">[</span><span class="pl-n">maxn</span><span class="pl-p">],</span> <span class="pl-n">fa</span><span class="pl-p">[</span><span class="pl-n">maxn</span><span class="pl-p">],</span> <span class="pl-n">cnt</span><span class="pl-p">,</span> <span class="pl-n">dep</span><span class="pl-p">[</span><span class="pl-n">maxn</span><span class="pl-p">],</span> <span class="pl-n">siz</span><span class="pl-p">[</span><span class="pl-n">maxn</span><span class="pl-p">],</span> <span class="pl-n">top</span><span class="pl-p">[</span><span class="pl-n">maxn</span><span class="pl-p">];</span>
<span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">res</span> <span class="pl-o">=</span> <span class="pl-mi">0</span><span class="pl-p">;</span>
<span class="pl-kt">void</span> <span class="pl-nf">Add</span><span class="pl-p">(</span><span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">u</span><span class="pl-p">,</span> <span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">v</span><span class="pl-p">)</span> <span class="pl-p">{</span>
    <span class="pl-n">h</span><span class="pl-p">[</span><span class="pl-o">++</span><span class="pl-n">tot</span><span class="pl-p">].</span><span class="pl-n">next</span> <span class="pl-o">=</span> <span class="pl-n">head</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">];</span>
    <span class="pl-n">h</span><span class="pl-p">[</span><span class="pl-n">tot</span><span class="pl-p">].</span><span class="pl-n">to</span> <span class="pl-o">=</span> <span class="pl-n">v</span><span class="pl-p">;</span>
    <span class="pl-n">head</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">]</span> <span class="pl-o">=</span> <span class="pl-n">tot</span><span class="pl-p">;</span>
<span class="pl-p">}</span>
<span class="pl-kt">void</span> <span class="pl-nf">pushdown</span><span class="pl-p">(</span><span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">rt</span><span class="pl-p">,</span> <span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">lenn</span><span class="pl-p">)</span> <span class="pl-p">{</span>
    <span class="pl-n">laz</span><span class="pl-p">[</span><span class="pl-n">rt</span> <span class="pl-o">&lt;&lt;</span> <span class="pl-mi">1</span><span class="pl-p">]</span> <span class="pl-o">+=</span> <span class="pl-n">laz</span><span class="pl-p">[</span><span class="pl-n">rt</span><span class="pl-p">];</span>
    <span class="pl-n">laz</span><span class="pl-p">[</span><span class="pl-n">rt</span> <span class="pl-o">&lt;&lt;</span> <span class="pl-mi">1</span> <span class="pl-o">|</span> <span class="pl-mi">1</span><span class="pl-p">]</span> <span class="pl-o">+=</span> <span class="pl-n">laz</span><span class="pl-p">[</span><span class="pl-n">rt</span><span class="pl-p">];</span>
    <span class="pl-n">a</span><span class="pl-p">[</span><span class="pl-n">rt</span> <span class="pl-o">&lt;&lt;</span> <span class="pl-mi">1</span><span class="pl-p">]</span> <span class="pl-o">+=</span> <span class="pl-n">laz</span><span class="pl-p">[</span><span class="pl-n">rt</span><span class="pl-p">]</span> <span class="pl-o">*</span> <span class="pl-p">(</span><span class="pl-n">lenn</span> <span class="pl-o">-</span> <span class="pl-p">(</span><span class="pl-n">lenn</span> <span class="pl-o">&gt;&gt;</span> <span class="pl-mi">1</span><span class="pl-p">));</span>
    <span class="pl-n">a</span><span class="pl-p">[</span><span class="pl-n">rt</span> <span class="pl-o">&lt;&lt;</span> <span class="pl-mi">1</span> <span class="pl-o">|</span> <span class="pl-mi">1</span><span class="pl-p">]</span> <span class="pl-o">+=</span> <span class="pl-n">laz</span><span class="pl-p">[</span><span class="pl-n">rt</span><span class="pl-p">]</span> <span class="pl-o">*</span> <span class="pl-p">(</span><span class="pl-n">lenn</span> <span class="pl-o">&gt;&gt;</span> <span class="pl-mi">1</span><span class="pl-p">);</span>
    <span class="pl-n">laz</span><span class="pl-p">[</span><span class="pl-n">rt</span><span class="pl-p">]</span> <span class="pl-o">=</span> <span class="pl-mi">0</span><span class="pl-p">;</span>
<span class="pl-p">}</span>
<span class="pl-kt">void</span> <span class="pl-nf">build</span><span class="pl-p">(</span><span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">rt</span><span class="pl-p">,</span> <span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">l</span><span class="pl-p">,</span> <span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">r</span><span class="pl-p">)</span> <span class="pl-p">{</span>
    <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">l</span> <span class="pl-o">==</span> <span class="pl-n">r</span><span class="pl-p">)</span> <span class="pl-k">return</span> <span class="pl-n">a</span><span class="pl-p">[</span><span class="pl-n">rt</span><span class="pl-p">]</span> <span class="pl-o">=</span> <span class="pl-n">wt</span><span class="pl-p">[</span><span class="pl-n">l</span><span class="pl-p">],</span> <span class="pl-kt">void</span><span class="pl-p">();</span>
    <span class="pl-n">build</span><span class="pl-p">(</span><span class="pl-n">lson</span><span class="pl-p">),</span> <span class="pl-n">build</span><span class="pl-p">(</span><span class="pl-n">rson</span><span class="pl-p">);</span>
    <span class="pl-n">a</span><span class="pl-p">[</span><span class="pl-n">rt</span><span class="pl-p">]</span> <span class="pl-o">=</span> <span class="pl-p">(</span><span class="pl-n">a</span><span class="pl-p">[</span><span class="pl-n">rt</span> <span class="pl-o">&lt;&lt;</span> <span class="pl-mi">1</span><span class="pl-p">]</span> <span class="pl-o">+</span> <span class="pl-n">a</span><span class="pl-p">[</span><span class="pl-n">rt</span> <span class="pl-o">&lt;&lt;</span> <span class="pl-mi">1</span> <span class="pl-o">|</span> <span class="pl-mi">1</span><span class="pl-p">]);</span>
<span class="pl-p">}</span>
<span class="pl-kt">void</span> <span class="pl-nf">query</span><span class="pl-p">(</span><span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">rt</span><span class="pl-p">,</span> <span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">l</span><span class="pl-p">,</span> <span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">r</span><span class="pl-p">,</span> <span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">L</span><span class="pl-p">,</span> <span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">R</span><span class="pl-p">)</span> <span class="pl-p">{</span>
    <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">L</span> <span class="pl-o">&lt;=</span> <span class="pl-n">l</span> <span class="pl-o">&amp;&amp;</span> <span class="pl-n">r</span> <span class="pl-o">&lt;=</span> <span class="pl-n">R</span><span class="pl-p">)</span> <span class="pl-k">return</span> <span class="pl-n">res</span> <span class="pl-o">+=</span> <span class="pl-n">a</span><span class="pl-p">[</span><span class="pl-n">rt</span><span class="pl-p">],</span> <span class="pl-kt">void</span><span class="pl-p">();</span>
    <span class="pl-k">else</span> <span class="pl-p">{</span>
        <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">laz</span><span class="pl-p">[</span><span class="pl-n">rt</span><span class="pl-p">])</span> <span class="pl-n">pushdown</span><span class="pl-p">(</span><span class="pl-n">rt</span><span class="pl-p">,</span> <span class="pl-n">len</span><span class="pl-p">);</span>
        <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">L</span> <span class="pl-o">&lt;=</span> <span class="pl-n">mid</span><span class="pl-p">)</span> <span class="pl-n">query</span><span class="pl-p">(</span><span class="pl-n">lson</span><span class="pl-p">,</span> <span class="pl-n">L</span><span class="pl-p">,</span> <span class="pl-n">R</span><span class="pl-p">);</span>
        <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">R</span> <span class="pl-o">&gt;</span> <span class="pl-n">mid</span><span class="pl-p">)</span> <span class="pl-n">query</span><span class="pl-p">(</span><span class="pl-n">rson</span><span class="pl-p">,</span> <span class="pl-n">L</span><span class="pl-p">,</span> <span class="pl-n">R</span><span class="pl-p">);</span>
    <span class="pl-p">}</span>
<span class="pl-p">}</span>
<span class="pl-kt">void</span> <span class="pl-nf">update</span><span class="pl-p">(</span><span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">rt</span><span class="pl-p">,</span> <span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">l</span><span class="pl-p">,</span> <span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">r</span><span class="pl-p">,</span> <span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">L</span><span class="pl-p">,</span> <span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">R</span><span class="pl-p">,</span> <span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">k</span><span class="pl-p">)</span> <span class="pl-p">{</span>
    <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">L</span> <span class="pl-o">&lt;=</span> <span class="pl-n">l</span> <span class="pl-o">&amp;&amp;</span> <span class="pl-n">r</span> <span class="pl-o">&lt;=</span> <span class="pl-n">R</span><span class="pl-p">)</span> <span class="pl-p">{</span>
        <span class="pl-n">laz</span><span class="pl-p">[</span><span class="pl-n">rt</span><span class="pl-p">]</span> <span class="pl-o">+=</span> <span class="pl-n">k</span><span class="pl-p">;</span>
        <span class="pl-n">a</span><span class="pl-p">[</span><span class="pl-n">rt</span><span class="pl-p">]</span> <span class="pl-o">+=</span> <span class="pl-n">k</span> <span class="pl-o">*</span> <span class="pl-n">len</span><span class="pl-p">;</span>
    <span class="pl-p">}</span> <span class="pl-k">else</span> <span class="pl-p">{</span>
        <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">laz</span><span class="pl-p">[</span><span class="pl-n">rt</span><span class="pl-p">])</span> <span class="pl-n">pushdown</span><span class="pl-p">(</span><span class="pl-n">rt</span><span class="pl-p">,</span> <span class="pl-n">len</span><span class="pl-p">);</span>
        <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">L</span> <span class="pl-o">&lt;=</span> <span class="pl-n">mid</span><span class="pl-p">)</span> <span class="pl-n">update</span><span class="pl-p">(</span><span class="pl-n">lson</span><span class="pl-p">,</span> <span class="pl-n">L</span><span class="pl-p">,</span> <span class="pl-n">R</span><span class="pl-p">,</span> <span class="pl-n">k</span><span class="pl-p">);</span>
        <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">R</span> <span class="pl-o">&gt;</span> <span class="pl-n">mid</span><span class="pl-p">)</span> <span class="pl-n">update</span><span class="pl-p">(</span><span class="pl-n">rson</span><span class="pl-p">,</span> <span class="pl-n">L</span><span class="pl-p">,</span> <span class="pl-n">R</span><span class="pl-p">,</span> <span class="pl-n">k</span><span class="pl-p">);</span>
        <span class="pl-n">a</span><span class="pl-p">[</span><span class="pl-n">rt</span><span class="pl-p">]</span> <span class="pl-o">=</span> <span class="pl-p">(</span><span class="pl-n">a</span><span class="pl-p">[</span><span class="pl-n">rt</span> <span class="pl-o">&lt;&lt;</span> <span class="pl-mi">1</span><span class="pl-p">]</span> <span class="pl-o">+</span> <span class="pl-n">a</span><span class="pl-p">[</span><span class="pl-n">rt</span> <span class="pl-o">&lt;&lt;</span> <span class="pl-mi">1</span> <span class="pl-o">|</span> <span class="pl-mi">1</span><span class="pl-p">]);</span>
    <span class="pl-p">}</span>
<span class="pl-p">}</span>
<span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-nf">qRange</span><span class="pl-p">(</span><span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">x</span><span class="pl-p">,</span> <span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">y</span><span class="pl-p">)</span> <span class="pl-p">{</span>
    <span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">ans</span> <span class="pl-o">=</span> <span class="pl-mi">0</span><span class="pl-p">;</span>
    <span class="pl-k">while</span> <span class="pl-p">(</span><span class="pl-n">top</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">]</span> <span class="pl-o">!=</span> <span class="pl-n">top</span><span class="pl-p">[</span><span class="pl-n">y</span><span class="pl-p">])</span> <span class="pl-p">{</span>
        <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">dep</span><span class="pl-p">[</span><span class="pl-n">top</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">]]</span> <span class="pl-o">&lt;</span> <span class="pl-n">dep</span><span class="pl-p">[</span><span class="pl-n">top</span><span class="pl-p">[</span><span class="pl-n">y</span><span class="pl-p">]])</span> <span class="pl-n">swap</span><span class="pl-p">(</span><span class="pl-n">x</span><span class="pl-p">,</span> <span class="pl-n">y</span><span class="pl-p">);</span>
        <span class="pl-n">res</span> <span class="pl-o">=</span> <span class="pl-mi">0</span><span class="pl-p">;</span>
        <span class="pl-n">query</span><span class="pl-p">(</span><span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-n">n</span><span class="pl-p">,</span> <span class="pl-n">id</span><span class="pl-p">[</span><span class="pl-n">top</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">]],</span> <span class="pl-n">id</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">]);</span>
        <span class="pl-n">ans</span> <span class="pl-o">+=</span> <span class="pl-n">res</span><span class="pl-p">;</span>
        <span class="pl-n">x</span> <span class="pl-o">=</span> <span class="pl-n">fa</span><span class="pl-p">[</span><span class="pl-n">top</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">]];</span>
    <span class="pl-p">}</span>
    <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">dep</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">]</span> <span class="pl-o">&gt;</span> <span class="pl-n">dep</span><span class="pl-p">[</span><span class="pl-n">y</span><span class="pl-p">])</span> <span class="pl-n">swap</span><span class="pl-p">(</span><span class="pl-n">x</span><span class="pl-p">,</span> <span class="pl-n">y</span><span class="pl-p">);</span>
    <span class="pl-n">res</span> <span class="pl-o">=</span> <span class="pl-mi">0</span><span class="pl-p">;</span>
    <span class="pl-n">query</span><span class="pl-p">(</span><span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-n">n</span><span class="pl-p">,</span> <span class="pl-n">id</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">],</span> <span class="pl-n">id</span><span class="pl-p">[</span><span class="pl-n">y</span><span class="pl-p">]);</span>
    <span class="pl-n">ans</span> <span class="pl-o">+=</span> <span class="pl-n">res</span><span class="pl-p">;</span>
    <span class="pl-k">return</span> <span class="pl-n">ans</span><span class="pl-p">;</span>
<span class="pl-p">}</span>
<span class="pl-kt">void</span> <span class="pl-nf">updSon</span><span class="pl-p">(</span><span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">x</span><span class="pl-p">,</span> <span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">k</span><span class="pl-p">)</span> <span class="pl-p">{</span>
    <span class="pl-n">update</span><span class="pl-p">(</span><span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-n">n</span><span class="pl-p">,</span> <span class="pl-n">id</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">],</span> <span class="pl-n">id</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">]</span> <span class="pl-o">+</span> <span class="pl-n">siz</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">]</span> <span class="pl-o">-</span> <span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-n">k</span><span class="pl-p">);</span>
<span class="pl-p">}</span>

<span class="pl-kt">void</span> <span class="pl-nf">dfs1</span><span class="pl-p">(</span><span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">x</span><span class="pl-p">,</span> <span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">f</span><span class="pl-p">,</span> <span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">deep</span><span class="pl-p">)</span> <span class="pl-p">{</span>
    <span class="pl-n">dep</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">]</span> <span class="pl-o">=</span> <span class="pl-n">deep</span><span class="pl-p">;</span>
    <span class="pl-n">fa</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">]</span> <span class="pl-o">=</span> <span class="pl-n">f</span><span class="pl-p">;</span>
    <span class="pl-n">siz</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">]</span> <span class="pl-o">=</span> <span class="pl-mi">1</span><span class="pl-p">;</span>
    <span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">maxson</span> <span class="pl-o">=</span> <span class="pl-o">-</span><span class="pl-mi">1</span><span class="pl-p">;</span>
    <span class="pl-k">for</span> <span class="pl-p">(</span><span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">i</span> <span class="pl-o">=</span> <span class="pl-n">head</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">];</span> <span class="pl-n">i</span><span class="pl-p">;</span> <span class="pl-n">i</span> <span class="pl-o">=</span> <span class="pl-n">h</span><span class="pl-p">[</span><span class="pl-n">i</span><span class="pl-p">].</span><span class="pl-n">next</span><span class="pl-p">)</span> <span class="pl-p">{</span>
        <span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">y</span> <span class="pl-o">=</span> <span class="pl-n">h</span><span class="pl-p">[</span><span class="pl-n">i</span><span class="pl-p">].</span><span class="pl-n">to</span><span class="pl-p">;</span>
        <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">y</span> <span class="pl-o">==</span> <span class="pl-n">f</span><span class="pl-p">)</span> <span class="pl-k">continue</span><span class="pl-p">;</span>
        <span class="pl-n">dfs1</span><span class="pl-p">(</span><span class="pl-n">y</span><span class="pl-p">,</span> <span class="pl-n">x</span><span class="pl-p">,</span> <span class="pl-n">deep</span> <span class="pl-o">+</span> <span class="pl-mi">1</span><span class="pl-p">);</span>
        <span class="pl-n">siz</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">]</span> <span class="pl-o">+=</span> <span class="pl-n">siz</span><span class="pl-p">[</span><span class="pl-n">y</span><span class="pl-p">];</span>
        <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">siz</span><span class="pl-p">[</span><span class="pl-n">y</span><span class="pl-p">]</span> <span class="pl-o">&gt;</span> <span class="pl-n">maxson</span><span class="pl-p">)</span> <span class="pl-n">son</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">]</span> <span class="pl-o">=</span> <span class="pl-n">y</span><span class="pl-p">,</span> <span class="pl-n">maxson</span> <span class="pl-o">=</span> <span class="pl-n">siz</span><span class="pl-p">[</span><span class="pl-n">y</span><span class="pl-p">];</span>
    <span class="pl-p">}</span>
<span class="pl-p">}</span>
<span class="pl-kt">void</span> <span class="pl-nf">dfs2</span><span class="pl-p">(</span><span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">x</span><span class="pl-p">,</span> <span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">topf</span><span class="pl-p">)</span> <span class="pl-p">{</span>
    <span class="pl-n">id</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">]</span> <span class="pl-o">=</span> <span class="pl-o">++</span><span class="pl-n">cnt</span><span class="pl-p">;</span>
    <span class="pl-n">wt</span><span class="pl-p">[</span><span class="pl-n">cnt</span><span class="pl-p">]</span> <span class="pl-o">=</span> <span class="pl-n">w</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">];</span>
    <span class="pl-n">top</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">]</span> <span class="pl-o">=</span> <span class="pl-n">topf</span><span class="pl-p">;</span>
    <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-o">!</span><span class="pl-n">son</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">])</span> <span class="pl-k">return</span><span class="pl-p">;</span>
    <span class="pl-n">dfs2</span><span class="pl-p">(</span><span class="pl-n">son</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">],</span> <span class="pl-n">topf</span><span class="pl-p">);</span>
    <span class="pl-k">for</span> <span class="pl-p">(</span><span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">i</span> <span class="pl-o">=</span> <span class="pl-n">head</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">];</span> <span class="pl-n">i</span><span class="pl-p">;</span> <span class="pl-n">i</span> <span class="pl-o">=</span> <span class="pl-n">h</span><span class="pl-p">[</span><span class="pl-n">i</span><span class="pl-p">].</span><span class="pl-n">next</span><span class="pl-p">)</span> <span class="pl-p">{</span>
        <span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">y</span> <span class="pl-o">=</span> <span class="pl-n">h</span><span class="pl-p">[</span><span class="pl-n">i</span><span class="pl-p">].</span><span class="pl-n">to</span><span class="pl-p">;</span>
        <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">y</span> <span class="pl-o">==</span> <span class="pl-n">fa</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">]</span> <span class="pl-o">||</span> <span class="pl-n">y</span> <span class="pl-o">==</span> <span class="pl-n">son</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">])</span> <span class="pl-k">continue</span><span class="pl-p">;</span>
        <span class="pl-n">dfs2</span><span class="pl-p">(</span><span class="pl-n">y</span><span class="pl-p">,</span> <span class="pl-n">y</span><span class="pl-p">);</span>
    <span class="pl-p">}</span>
<span class="pl-p">}</span>
<span class="pl-kt">int</span> <span class="pl-nf">main</span><span class="pl-p">()</span> <span class="pl-p">{</span>
    <span class="pl-n">n</span> <span class="pl-o">=</span> <span class="pl-n">gi</span><span class="pl-p">(),</span> <span class="pl-n">m</span> <span class="pl-o">=</span> <span class="pl-n">gi</span><span class="pl-p">();</span>
    <span class="pl-k">for</span> <span class="pl-p">(</span><span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">i</span> <span class="pl-o">=</span> <span class="pl-mi">1</span><span class="pl-p">;</span> <span class="pl-n">i</span> <span class="pl-o">&lt;=</span> <span class="pl-n">n</span><span class="pl-p">;</span> <span class="pl-n">i</span><span class="pl-o">++</span><span class="pl-p">)</span> <span class="pl-n">w</span><span class="pl-p">[</span><span class="pl-n">i</span><span class="pl-p">]</span> <span class="pl-o">=</span> <span class="pl-n">gi</span><span class="pl-p">();</span>
    <span class="pl-k">for</span> <span class="pl-p">(</span><span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">i</span> <span class="pl-o">=</span> <span class="pl-mi">1</span><span class="pl-p">;</span> <span class="pl-n">i</span> <span class="pl-o">&lt;</span> <span class="pl-n">n</span><span class="pl-p">;</span> <span class="pl-n">i</span><span class="pl-o">++</span><span class="pl-p">)</span> <span class="pl-p">{</span>
        <span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">a</span><span class="pl-p">,</span> <span class="pl-n">b</span><span class="pl-p">;</span>
        <span class="pl-n">a</span> <span class="pl-o">=</span> <span class="pl-n">gi</span><span class="pl-p">(),</span> <span class="pl-n">b</span> <span class="pl-o">=</span> <span class="pl-n">gi</span><span class="pl-p">();</span>
        <span class="pl-n">Add</span><span class="pl-p">(</span><span class="pl-n">a</span><span class="pl-p">,</span> <span class="pl-n">b</span><span class="pl-p">),</span> <span class="pl-n">Add</span><span class="pl-p">(</span><span class="pl-n">b</span><span class="pl-p">,</span> <span class="pl-n">a</span><span class="pl-p">);</span>
    <span class="pl-p">}</span>
    <span class="pl-n">dfs1</span><span class="pl-p">(</span><span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-mi">0</span><span class="pl-p">,</span> <span class="pl-mi">1</span><span class="pl-p">),</span> <span class="pl-n">dfs2</span><span class="pl-p">(</span><span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-mi">1</span><span class="pl-p">);</span>
    <span class="pl-n">build</span><span class="pl-p">(</span><span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-n">n</span><span class="pl-p">);</span>
    <span class="pl-k">while</span> <span class="pl-p">(</span><span class="pl-n">m</span><span class="pl-o">--</span><span class="pl-p">)</span> <span class="pl-p">{</span>
        <span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">k</span><span class="pl-p">,</span> <span class="pl-n">x</span><span class="pl-p">,</span> <span class="pl-n">y</span><span class="pl-p">,</span> <span class="pl-n">z</span><span class="pl-p">;</span>
        <span class="pl-n">k</span> <span class="pl-o">=</span> <span class="pl-n">gi</span><span class="pl-p">();</span>
        <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">k</span> <span class="pl-o">==</span> <span class="pl-mi">1</span><span class="pl-p">)</span> <span class="pl-p">{</span>
            <span class="pl-n">x</span> <span class="pl-o">=</span> <span class="pl-n">gi</span><span class="pl-p">(),</span> <span class="pl-n">y</span> <span class="pl-o">=</span> <span class="pl-n">gi</span><span class="pl-p">();</span>
            <span class="pl-n">update</span><span class="pl-p">(</span><span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-n">n</span><span class="pl-p">,</span> <span class="pl-n">id</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">],</span> <span class="pl-n">id</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">],</span> <span class="pl-n">y</span><span class="pl-p">);</span>
        <span class="pl-p">}</span> <span class="pl-k">else</span> <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">k</span> <span class="pl-o">==</span> <span class="pl-mi">2</span><span class="pl-p">)</span> <span class="pl-p">{</span>
            <span class="pl-n">x</span> <span class="pl-o">=</span> <span class="pl-n">gi</span><span class="pl-p">(),</span> <span class="pl-n">y</span> <span class="pl-o">=</span> <span class="pl-n">gi</span><span class="pl-p">();</span>
            <span class="pl-n">updSon</span><span class="pl-p">(</span><span class="pl-n">x</span><span class="pl-p">,</span> <span class="pl-n">y</span><span class="pl-p">);</span>
        <span class="pl-p">}</span> <span class="pl-k">else</span> <span class="pl-p">{</span>
            <span class="pl-n">x</span> <span class="pl-o">=</span> <span class="pl-n">gi</span><span class="pl-p">();</span>
            <span class="pl-n">printf</span><span class="pl-p">(</span><span class="pl-s">&quot;%lld</span><span class="pl-se">\n</span><span class="pl-s">&quot;</span><span class="pl-p">,</span> <span class="pl-n">qRange</span><span class="pl-p">(</span><span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-n">x</span><span class="pl-p">));</span>
        <span class="pl-p">}</span>
    <span class="pl-p">}</span>
    <span class="pl-k">return</span> <span class="pl-mi">0</span><span class="pl-p">;</span>
<span class="pl-p">}</span>
</cpde></pre></div>